package com.sum.slike.simple;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.sum.slike.SuperLikeLayout;

public class SimpleActivity extends AppCompatActivity {

    SuperLikeLayout superLikeLayout;
    private long lastClickTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler = new Handler();
        superLikeLayout = findViewById(R.id.super_like_layout);
        superLikeLayout.setProvider(BitmapProviderFactory.getHDProvider(this));
        findViewById(R.id.like_btn).setOnClickListener(new View.OnClickListener() {
            long duration = 2000;
            long lastClickTime;

            @Override
            public void onClick(View v) {
                clickView = v;
//                Log.e("xxxx","onClick");
//                if (System.currentTimeMillis() - lastClickTime > duration) { // 防抖
//                    v.setSelected(!v.isSelected());
//                }
//                lastClickTime = System.currentTimeMillis();
//                if (v.isSelected()) {
                launchEmoji();
                //  }
            }
        });


        findViewById(R.id.like_btn).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                LongTouchSendCmd(SimpleActivity.this, motionEvent, view);
                return false;
            }
        });
    }

    private Handler handler;
    private boolean longPress = false;
    private View clickView;


    long downTime;
    private boolean isTouchDown;

    private void LongTouchSendCmd(final Activity activity, MotionEvent event, final View v) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                Log.e("xxxx", "ACTION_DOWN");
                isTouchDown = true;
                clickView = v;
                handler.postDelayed(runnable, 500);
                break;
            }
            case MotionEvent.ACTION_UP: {
                Log.e("xxxx", "ACTION_UP");
                isTouchDown = false;
                longPress = false;
            }
        }

    }

    private void launchEmoji() {
        if (System.currentTimeMillis() - lastClickTime > 50) { // 防抖
            int x = (int) (clickView.getX() + clickView.getWidth() / 2);
            int y = (int) (clickView.getY() + clickView.getHeight() / 2);
            superLikeLayout.launch(x, y);
        }
        lastClickTime = System.currentTimeMillis();
    }


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (isTouchDown) {
                longPress = true;
                handler.postDelayed(runnable2, 100);
            }
        }
    };

    Runnable runnable2 = new Runnable() {
        @Override
        public void run() {
            if (longPress && clickView != null) {
                launchEmoji();
                handler.postDelayed(runnable2, 100);
            }
        }
    };
}
